package com.chenfan.ccp.plug.business.parser;

import com.alibaba.fastjson.JSONObject;
import com.chenfan.ccp.constants.Constants;
import com.chenfan.ccp.entity.BaseUserVO;
import lombok.extern.slf4j.Slf4j;
import net.logstash.logback.encoder.org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * @author xutong
 */
@Slf4j
public class DefaultUserIdParser implements UserIdParser {

    @Override
    public BaseUserVO parserUserId(HttpServletRequest request, HttpServletResponse response) {
        //TODO 获取系统中默认的userid获取方式
        String header = request.getHeader(Constants.CURRENT_USER);
        if (StringUtils.isNotBlank(header)) {
            try {
                String decode = URLDecoder.decode(header, StandardCharsets.UTF_8.name());
                BaseUserVO baseUserVO = JSONObject.parseObject(decode, BaseUserVO.class);
                return baseUserVO;
            } catch (UnsupportedEncodingException e) {
                log.error("系统用户名称解析失败");
            }
        }
        return new BaseUserVO();
    }
}
